package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.StringTools;
import com.example.demo.common.UserSessionTools;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserEntity;
import com.example.demo.entity.vo.ArticleInfoVO;
import com.example.demo.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest req) {
        // 1. 非空校验
        if (articleInfo == null
                || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(-1, "参数异常");
        }
        // 2. 得到 UID
        UserEntity user = UserSessionTools.getLoginUser(req);
        articleInfo.setUid(user.getId());
        // 3. 持久化, 返回结果给前端
        int result = articleService.add(articleInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/getDetailById")
    public AjaxResult getDetailByIdAndUid(Integer id, HttpServletRequest req) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
        UserEntity user = UserSessionTools.getLoginUser(req);

        return AjaxResult.success(articleService.getDetailByIdAndUid(id, user.getId()));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo, HttpServletRequest req) {
        // 1. 非空校验
        if (null == articleInfo || articleInfo.getId() <= 0
                || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(-1, "参数有误");
        }

        // 2. 获取登录用户 id. 填充到对象中
        UserEntity user = UserSessionTools.getLoginUser(req);
        articleInfo.setUid(user.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        int res = articleService.update(articleInfo);
        return AjaxResult.success(res);
    }

    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数有误!");
        }
        ArticleInfoVO articleInfoVO = articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }

    @RequestMapping("/addrcount")
    public AjaxResult addRCount(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数有误!");
        }
        int res = articleService.addRCount(id);
        return AjaxResult.success(res);
    }

    @RequestMapping("/mylist")
    public AjaxResult getListByUid(HttpServletRequest req) {
        UserEntity user = UserSessionTools.getLoginUser(req);
        List<ArticleInfo> list = articleService.getListByUid(user.getId());
        list.stream().parallel().forEach(item -> {
            item.setContent(StringTools.subLength(item.getContent(), 150));
        });
        // TODO: 将 md 里面的语法去掉
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id, HttpServletRequest req) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数错误!");
        }
        UserEntity user = UserSessionTools.getLoginUser(req);
        int res = articleService.del(id, user.getId());

        return AjaxResult.success(res);
    }

    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize, Integer pageIndex) {
        // 默认值处理
        if (pageSize == null || pageSize <= 0) {
            pageSize = 2;
        }
        if (pageIndex == null || pageIndex < 1) {
            pageIndex = 1;
        }

        int offset = (pageIndex - 1) * pageSize;
        List<ArticleInfo> list = articleService.getListByPage(pageSize, offset);
        // 多线程并发循环
        list.stream().parallel().forEach(item -> {
            item.setContent(StringTools.subLength(item.getContent(), 150));
        });

        return AjaxResult.success(list);
    }

    @RequestMapping("/getcount")
    public AjaxResult getCount() {
        return AjaxResult.success(articleService.getCount());
    }
}
